// pages/home_new/home_detail.js
const app = getApp();
var tools = require('../../utils/util.js');
var router = require("../../router.js");
var WxParse = require('../../wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    column_id:'',
    types:'',
    column_res: '', 
    grade:'',
    price:'',
    order_no:'',
    //下面三个是弹框需要的内容
    show_confirm_box: false,
    fadeIn: '',
    show_tj: false,//是否显示推荐信息
    new_prices:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  close_mask() {  //关闭推荐信息遮罩层    
    var animation = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    this.animation = animation;
    animation.translate(0, 0).opacity(0).step()
    this.setData({
      animationData: animation.export()
    })
    setTimeout(() => {
      this.setData({
        show_tj: false
      })
    }, 300)
  },
  open_confirm_boxs:function(e){
    var that = this;
    var jitian = e.target.dataset.id;
    var sy_vip3 = e.target.id;
    var phone = wx.getStorageSync('userInfo')['phone'];
    if (!phone){
      wx.showModal({
        title: '提示',
        content: '前往完善你的个人信息',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../edit_user_info/edit_user_info',
            })
          }else{}
        }
      })
      return false 
    }
    wx.showModal({
      title: '提示',
      content: '你确定报名吗？',
      success:function(res){
        if(res.confirm){
          app.request({
            url: router.expand.my_order,
            method: "POST",
            data: {
              user_id: wx.getStorageSync("userInfo")['id'],
              expand_id: that.data.column_res.id,
              sy_id: that.data.column_res.column_ids,
              jitian: jitian,
              sy_vip3: sy_vip3,
              types: that.data.types,
            },
            success: function (res) {
              console.log('resres',res)
              if (res == 1){
                wx.showModal({
                  title: '报名成功',
                  content: '是否前往我的报名中心？',
                  success:function(res){
                    if(res.confirm){
                      wx.navigateTo({
                        url: '../my_bm/my_bm',
                      })
                    }
                  } 
                })
              } else if (res == -2){
                wx.showToast({
                  title: '今天已经报过名',
                })
              }else{
                wx.showToast({
                  title: '稍后重试',
                })
              }
            }
          })
        }
        if(res.cancel){
          console.log('用户点击取消')
        }
      }
    })
  },
  // 生成订单->打开支付确定弹窗
  open_confirm_box(e){
    let that = this;
    let jitian = e.target.dataset.id;
    let grade = that.data.grade;
    // if (grade == 1 && jitian == 1){
    //   var price = that.data.column_res.price
    // } else if (grade == 1 && jitian == 2){
    //   var price = that.data.column_res.prices
    // } else if (grade == 2 && jitian == 1) {
    //   var price = that.data.column_res.vip_price
    // } else if (grade == 2 && jitian == 2) {
    //   var price = that.data.column_res.vip_prices
    // }
    if (jitian == 1) {
      var price = that.data.column_res.price
    } else if (jitian == 2) {
      var price = that.data.column_res.prices
    }
    var phone = wx.getStorageSync('userInfo')['phone'];
    if (!phone) {
      wx.showModal({
        title: '提示',
        content: '前往完善你的个人信息',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../edit_user_info/edit_user_info',
            })
          } else { }
        }
      })
      return false
    }
    app.request({
      url: router.expand.my_orders,
      method: "POST",
      data: {
        expand_id: that.data.column_res.id,
        sy_id: that.data.column_res.column_ids,
        user_id: wx.getStorageSync("userInfo")['id'],
        jitian: jitian,
        price: price,
        types: that.data.types,
      },
      success: function (data) {
        console.log(data);
        if (data.status == true) {
          wx.showToast({
            title: '操作成功',
          })
          if (data.info) {
            that.setData({
              pay_info: data.info,
              order_no: data.order_no
            });
          }
          that.setData({
            show_confirm_box: true,
            price: price
          });
          let animation = wx.createAnimation({
            duration: '300',
            timingFunction: 'ease',
          })
          animation.translate(0, -50).opacity(1).step()
          that.setData({
            animationData: animation.export()
          })
          let animation2 = wx.createAnimation({
            duration: '300',
            timingFunction: 'ease',
          })
          animation2.opacity(1).step()
          that.setData({
            fadeIn: animation2.export(),
            jitian: jitian,
          })
        } else if (data.status == -2){
          wx.showToast({
            title: '今天已经报过名',
          })
        }else{
          wx.showToast({
            title: '稍后重试',
          })
        }
      }  
    })
  },
  // 关闭支付确定弹窗
  clsoe_confirm_box() {
    let animation = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation.translate(0, 0).opacity(0).step()
    this.setData({
      animationData: animation.export()
    })
    let animation2 = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation2.opacity(0).step()
    this.setData({
      fadeIn: animation2.export()
    })
    setTimeout(() => {
      this.setData({
        show_confirm_box: false
      });
    }, 300)
  },
  onLoad: function (options) {
    console.log(options)
    var that = this
    that.setData({
      column_id : options.id,
      types: options.types
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var column_id = that.data.column_id;
    var types = that.data.types;
    app.request({
      url: router.index.column_info,
      method: "POST",
      data: { column_id: column_id, types: types},
      success: function (res) {
        console.log('res',res)
        if(res.data == 0){

        }else{
          that.setData({
            column_res: res.data,
            grade: wx.getStorageSync('grade')
          });
        }
        WxParse.wxParse('columnss', 'html', res.data.info, that, 5);
      }
    })
  },
  pay: function (data) {
    var payInfo = this.data.pay_info;
    if (!payInfo){
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      });
      return false;
    } 
    var that = this; 
    wx.requestPayment({
      'timeStamp': payInfo.timeStamp,
      'nonceStr': payInfo.nonceStr,
      'package': payInfo.package,
      'signType': payInfo.signType,
      'paySign': payInfo.paySign,
      'success': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:ok') {
          that.setData({
            show_confirm_box: false
          });
          wx.showToast({
            title: '支付成功',
            icon: 'none',
            success: function () {
              app.request({
                url: router.expand.callbacks,
                method: "POST",
                data: {
                  order_no: that.data.order_no,
                  types: that.data.types,
                  uid: wx.getStorageSync("userInfo")['id'],
                  new_prices: that.data.price,
                },
                success: function (res) {
                  console.log('resres',res)

                  if (res){
                    wx.showModal({
                      title: '报名成功',
                      content: '是否前往我的报名中心？',
                      success: function (res) {
                        if (res.confirm) {
                          app.navTo('../my_bm/my_bm');
                        } else { }
                      }
                    })
                  }
                }
              })
            }
          })        
        }
      },
      'fail': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:fail cancel') {
          wx.showToast({
            title: '取消支付',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      },
      'complete': function (res) {
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})